function create_binned_data(raster_file_directory_name, save_prefix_name, bin_width, step_size, start_time, end_time)

% This function takes the name of a directory that contains files in raster-format 
%  and creates data that is in binned-format from these files.  The arguments to this 
%  function are:
%
%  1. raster_file_directory_name: the path to the directory that contains the files
%      in raster format.
%
%  2. save_prefix_name: the beginning of a file name (possibly including a directory name)
%      that specifies the name that the binned data should be saved as.  Appended on 
%      to the end of this name when the file is saved is the bin width, step size, 
%      and possibly start and end times used in the binning.my_raster_file_directory/
%      
%  3. bin_width:  the bin size that is averaged over when creating binned-format features
%      (e.g., if the raster file has spike times given with millisecond position, and
%      bin_width = 500, then the binned-format data will contain average firing rates
%      (i.e., the spike-count rate) in 500 ms bins).  
%      
%  4. step_size: specifies the sampling interval between successive binned-data points
%     (e.g., if the raster file has spike times given with millisecond position, 
%     and if step_size = 50, then a binned data point will be computed at 50ms intervals.
%
%  Optional arguments:
%
%  5. start_time:  This specifies the time to start the binning process.  If 
%      this argument is not set, then the binning will start with the first 
%      data point in the raster-file.
%
%  6. end_time:  This specifies the time when to end the binning process. If 
%      this argument is not set, then the binning will end with the last  
%      data point in the raster-file.
%
%  This function returns no outputs, but instead saves a file in the directory specified.
%
%
%  Example:  
%    
%   Suppose we had a directory called my_raster_file_directory/ that contained a number
%   of files in raster-data format from the spike times of neurons specified at 1 ms 
%   resolution.  Then running:
%
%   create_binned_data('my_raster_file_directory/', 'my_save_dir/binned_data', 150, 50, 200, 1000)
%    
%   will create a file in the binned_data_150ms_bins_50ms_sampled_200start_time_1000end_time
%   that will be saved in the directory my_save_dir/.  This file will contain the average
%   firing rate calculated over 150 ms intervals for all neurons in the directory 
%   my_raster_file_directory/.  The firing rates will be sampled every 50 ms, starting
%   200 ms into the raster-file data and ending 1000 ms into the raster-file data.
%



%==========================================================================

%     This code is part of the Neural Decoding Toolbox.
%     Copyright (C) 2011 by Ethan Meyers (emeyers@mit.edu)
% 
%     This program is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     (at your option) any later version.
% 
%     This program is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
% 
%     You should have received a copy of the GNU General Public License
%     along with this program.  If not, see <http://www.gnu.org/licenses/>.
    
%========================================================================== 




%  fix the directory name in case there it does not end with a slash
last_char = raster_file_directory_name(end)
if (~strcmp(last_char, '/') + ~strcmp(last_char, '\')) == 0
    raster_file_directory_name = [raster_file_directory_name '/']
end



raster_file_dir = dir([raster_file_directory_name '*.mat']);



if nargin < 5
    start_time = 1;
end
if nargin < 6
    load([raster_file_directory_name raster_file_dir(1).name]);
    end_time = size(rasterData, 2);  
end




% go through all the files and bin them
for i = 1:length(raster_file_dir)

   i 

   load([raster_file_directory_name raster_file_dir(i).name]);  

   
   curr_binned_data = bin_one_site(rasterData, bin_width, step_size, start_time, end_time);  % use the below helper function to bin the data
   
  
   the_data{i} = curr_binned_data;
   
   
   % save all the labels
   the_label_names = fieldnames(all_labels_onesite);
   for iLabel = 1:length(the_label_names)
      eval(['all_labels.' the_label_names{iLabel} '{i} = all_labels_onesite.' the_label_names{iLabel} ';']); 
   end
       
   
   % save any extra neuron info 
   if ~isempty(all_info_onesite)
       the_info_field_names = fieldnames(all_info_onesite);
       for iInfo = 1:length(the_info_field_names)
       
          if isstr( eval(['all_info_onesite.' the_info_field_names{iInfo}]))
              eval(['all_site_info.' the_info_field_names{iInfo} '{i} = all_info_onesite.' the_info_field_names{iInfo} ';']); 
          else
              eval(['all_site_info.' the_info_field_names{iInfo} '(i, :) = all_info_onesite.' the_info_field_names{iInfo} ';']);   % might run into problems with this so above line could be more useful
          end
       
       end
       
   else
       all_site_info = [];
   end
   

end




if nargin < 5
    save_name = [save_prefix_name '_' num2str(bin_width) 'ms_bins_' num2str(step_size) 'ms_sampled']; 
else
    save_name = [save_prefix_name '_' num2str(bin_width) 'ms_bins_' num2str(step_size) 'ms_sampled_' num2str(start_time) 'start_time_' num2str(end_time) 'end_time']; 
end



all_site_info.binning_info.bin_width = bin_width;  
all_site_info.binning_info.step_size = step_size; 
all_site_info.binning_info.start_time = start_time;
all_site_info.binning_info.end_time = end_time;


save(save_name, 'the_data', 'all_labels', 'all_site_info');

    






function  binned_data = bin_one_site(flatdata, bin_width, step_size, start_time, end_time)  
% a helper function that bins the data for one site


  c = 1;
  for k = start_time:step_size:(end_time - bin_width  + 1) 
      
                  
       binned_data(:, c) = mean(flatdata(:, k:k+ bin_width - 1), 2);
       
       c = c + 1;
       
  end

  


    
    

            
        
     
   


